package cn.eng.user.service.impl;

import cn.eng.basic.commonenum.ErrorCode;
import cn.eng.basic.domain.dto.PhoneRegisterDto;
import cn.eng.basic.exception.BusinessException;
import cn.eng.basic.utils.Md5Utils;
import cn.eng.basic.utils.StrUtils;
import cn.eng.user.domain.Logininfo;
import cn.eng.user.domain.User;
import cn.eng.user.mapper.LogininfoMapper;
import cn.eng.user.mapper.UserMapper;
import cn.eng.user.service.IUserService;
import cn.eng.basic.service.impl.BaseServiceImpl;
import io.swagger.models.auth.In;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 业务实现类：
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private LogininfoMapper logininfoMapper;
    @Autowired
    private UserMapper userMapper;
    /**
     * 手机注册
     * @param phoneRegisterDto
     */
    @Override
    @Transactional
    public void phoneRegister(PhoneRegisterDto phoneRegisterDto) {
        String phone = phoneRegisterDto.getPhone();//获取电话
        String password = phoneRegisterDto.getPassword();//获取密码
        String phoneCode = phoneRegisterDto.getPhoneCode();//获取用户传入的手机验证码
        //空值检验
        //校验手机验证码验证码是否过期
        Object  phoneCodeValue= redisTemplate.opsForValue().get("register:" + phone);//获取redis里面的手机验证码
        if (!phoneCode.equals(phoneCodeValue.toString().split(":")[0])){
            throw new BusinessException(ErrorCode.PHONECODE_ATYPISM.getCode());
        }
        //校验两次密码是否一致
        if(!phoneRegisterDto.getPassword().equals(phoneRegisterDto.getPasswordRepeat())){
            throw new BusinessException(ErrorCode.PASSWORD_ERROR.getCode());
        }
        //判断手机是否已注册
        User userData = userMapper.loadByPhone(phone);
        if (userData!=null){
            throw new BusinessException(ErrorCode.PHONE_REGISTER.getCode());
        }
        User user =phoneRegisterDto2User(phone,password);
        Logininfo logininfo = phoneRegisterDto2Logininfo(user);
        //添加数据到logininfo表
        logininfoMapper.save(logininfo);
        //添加logininfo的自增id到user表
        user.setLogininfoId(logininfo.getId());
        //添加数据到user
        userMapper.save(user);

    }

    /**添加logininfo数据  封装**/
    private Logininfo phoneRegisterDto2Logininfo(User user) {
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(user,logininfo);
        logininfo.setType(1);
        logininfo.setDisable(true);
        return logininfo;
    }

    /**添加user数据  封装**/
    private User phoneRegisterDto2User(String phone, String password) {
        User user = new User();
        user.setUsername(phone);
        user.setPhone(phone);
        String salt = StrUtils.getComplexRandomString(32);//使用工具获取32位的随机数 盐值
        String md5Password = Md5Utils.encrypByMd5(password + salt);//加密之后的密码
        user.setSalt(salt);
        user.setState(1);
        user.setCreatetime(new Date());
        user.setPassword(md5Password);
        return user;
    }

}
